<?php
	class VideoAction extends AdminAction{
	
			//搜索
			Public function so(){
				$video=M('Video');
				
				//搜索标题
				$so['title']=array('like','%'.$_GET['title'].'%');
				//返回内容
				$result=$this->_page($video,10,$so);
				
				$this->assign('list',$result);

				//输出select栏目选项
				$typelist=$this->typeselect(3);
				$this->assign('typelist',$typelist);

				$this->display('index');

			}

			//按分类查看内容
			public function type(){
				$video=M('Video');

				//查询条件
				$where['typeid']=$_GET['typeid'];
				$result=$this->_page($video,10,$where);
				$this->assign('list',$result);

				//输出select栏目选项
				$typelist=$this->typeselect(3);
				$this->assign('typelist',$typelist);
				//栏目选项默认值
				$this->assign('selected',$_GET['typeid']);

				$this->display('index');
			}

			//视频列表
			Public function index(){
				$video=M('Video');
				$menu=M('Menu');
				$admin=M('Admin');

				$list=$this->_page($video,10);

				for($i=0,$j=count($list);$i<$j;$i++){
					$list[$i]['menuname']=$menu->where(array('typeid'=>$list[$i]['typeid']))->field('name')->find(); 	//获得栏目名
					$list[$i]['zuozhe']=$admin->where(array('uid'=>$list[$i]['uid']))->field('username')->find();		//获得作者
				}

				//输出select栏目选项
				$typelist=$this->typeselect(3);
				$this->assign('typelist',$typelist);

				$this->assign('list',$list);
				
				$this->display();
			}
			
			//视频发布页
			Public function add(){
				$menu=new MenuModel;
				//仅输出文章模型的栏目
				$res=$menu->menulist(3);
				//按顺序遍历出栏目ID和栏目名
				$list=menus($res);

				//获得视频编辑的html表单
				$videotype=new VideotypeModel;
				$swfhtml=$videotype->swfhtml();		//swf视频编辑表单
				$this->assign('swfhtml',$swfhtml);	//输出视频表单HTML代码
				$vhtml=$videotype->vhtml();			//其它视频编辑表单HTML代码
				foreach($vhtml as $k=>$v){
					$this->assign($k,$v);
				}
		
				$addtime=date('Y-m-d H:i:s',time());
				$this->assign('stick',stick());
				$this->assign('disabled',disabled());
				$this->assign('menu',$list);			//输出栏目列表
				$this->assign('addtime',$addtime);		//输出添加时间
				$this->assign('groupids',groupids());	//输出浏览权限
				$this->display();
			}
			
			//视频添加
			Public function adding(){
				$video=M('Video');
				$videoAttach=M('VideoAttach');
				$videourl=M('Videourl');

				if(empty($_POST['title'])){
					$this->error('您忘了填写标题了哦！');
				}
				
				if(empty($_POST['content'])){
					$this->error('您忘了填写内容了哦！');
				}

				//数据转换
				$_POST['seotitle']=$_POST['seotitle']?$_POST['seotitle']:$_POST['title'];
				$_POST['playtime']=strtotime($_POST['playtime']);
				$_POST['addtime']=strtotime($_POST['addtime']);
				
				for($i=0,$j=count($_POST['stick']);$i<$j;$i++){		//自定义状态值
					$stick[$i]=$_POST['stick'][$i];
				}
				$_POST['stick']=implode(',',$stick);

				//添加数据
				$_POST['vid']=$video->add($_POST);	//添加主表
				
				$res1=$videoAttach->add($_POST);	//添加副表
				
				for($i=0,$j=count($_POST['vtype']);$i<$j;$i++){
					$data['vid']=$_POST['vid'];
					$data['vurl']=$_POST['vurl'][$i];
					$data['vname']=$_POST['vname'][$i];
					$data['vtype']=$_POST['vtype'][$i];
					$res2=$videourl->add($data);
				}


				if($_POST['vid'] || $res1 || $res2){
					$this->success('发布成功！');
				}else{
					$this->error('发布失败啦！');
				}
	
			}
			
			//视频编辑页
			Public function edit(){
				$video=M('Video');
				$videoAttach=M('VideoAttach');
				$videourl=M('Videourl');
				$menu=M('Menu');
			
				$menu=new MenuModel;
				//仅输出文章模型的栏目
				$res=$menu->menulist(3);
				//按顺序遍历出栏目ID和栏目名
				$list4=menus($res);	
				
				$list1=$video->find($_GET['id']);
				$list2=$videoAttach->where(array('vid'=>$_GET['id']))->find();
				$list3=$videourl->where(array('vid'=>$_GET['id']))->getfield('videoid,vid,vurl,vname,vtype');
				
				$list=array_merge($list1,$list2);
				$list['addtime']=date("Y-m-d H:i:s",$list['addtime']);
				$list['playtime']=date("Y-m-d H:i:s",$list['playtime']);
				$list['content']=stripslashes($list['content']);
				$stick=explode(',',$list['stick']);

				//缩略图		
				$thumb=$this->litimg($list['pic']);
				$thumb=__ROOT__.'/Uploads/'.$thumb;
				$this->assign('thumb',$thumb);

				$swf=array();$qvod=array();$baidu=array();$xunlei=array();

				$vtype=array(1,2,3,4,5,6,7,8,9);
				$i=0;
				foreach($list3 as $k => $v){
					foreach ($v as $a){
						if(in_array($a,$vtype)){$swf[$i]=$v;}if($a=='q'){$qvod[$i]=$v;}if($a=='b'){$baidu[$i]=$v;}if($a=='x'){$xunlei[$i]=$v;}$i++;
					}
				}

				//获得视频编辑的html表单
				$videotype=new VideotypeModel;
				$swfhtml=$videotype->swfhtml();		//swf视频编辑表单
				$this->assign('swfhtml',$swfhtml);	//输出视频表单HTML代码
				$vhtml=$videotype->vhtml();			//其它视频编辑表单HTML代码
				foreach($vhtml as $k=>$v){
					$this->assign($k,$v);
				}
				$option=$videotype->swftype();		//swf子分类数组

				//dump($swf);
				$this->assign('swf',$swf);
				$this->assign('qvod',$qvod);
				$this->assign('xunlei',$xunlei);
				$this->assign('baidu',$baidu);
				$this->assign('option',$option);
				$this->assign('disabled',disabled());
				$this->assign('menu',$list4);
				$this->assign('stickvalue',$stick);
				$this->assign('stick',stick());
				$this->assign('groupids',groupids());				//输出浏览权限
				$this->assign('groupidsvalue',$list['groupids_view']);	//浏览权限默认值
				$this->assign('allow',allow());						//是否允许评论
				$this->assign('allowvalue',$list['allow_comment']);	//评论默认值
				$this->assign('list',$list);
				$this->display();
			}
			
			//视频更新
			Public function editing(){
				$video=M('Video');
				$videoAttach=M('VideoAttach');
				$videourl=M('Videourl');
				
				//数据转换
				$_POST['seotitle']=$_POST['seotitle']?$_POST['seotitle']:$_POST['title'];
				$_POST['playtime']=strtotime($_POST['playtime']);
				$_POST['addtime']=strtotime($_POST['addtime']);

				for($i=0,$j=count($_POST['stick']);$i<$j;$i++){		//自定义状态值
					$stick[$i]=$_POST['stick'][$i];
				}
				$_POST['stick']=implode(',',$stick);

				//如果没有缩略图则销毁变量
				if(empty($_POST['pic'])){
					unset($_POST['pic']);
				}

				$res1=$video->where(array('id'=>$_POST['id']))->save($_POST);
				$res2=$videoAttach->where(array('vid'=>$_POST['id']))->save($_POST);
				
				$list=$videourl->where(array('vid'=>$_POST['id']))->getfield('videoid,vid');
				
				//添加视频|更新视频
				$list1=array();$n=0;
				for($i=0,$j=count($_POST['vtype']);$i<$j;$i++){
					$data['vid']=$_POST['id'];
					$data['videoid']=$_POST['videoid'][$i];
					$data['vurl']=$_POST['vurl'][$i];
					$data['vname']=$_POST['vname'][$i];
					$data['vtype']=$_POST['vtype'][$i];	
					
					if(empty($_POST['videoid'][$i])){
						$add=$videourl->add($data);
					}else{
						$list1[$n]=$data['videoid'];$n++;
						$up=$videourl->where(array('videoid'=>$data['videoid']))->save($data);
					}
				}
				
				//删除视频
				$list=array_keys($list);
				for($i=0,$j=count($list);$i<$j;$i++){
					if(!in_array($list[$i],$list1)){
						$del=$videourl->where(array('videoid'=>$list[$i]))->delete();
					}
				}
				
				if($res1 || $res2 || $del || $update==0 || $add){
					$this->success('编辑成功！');
				}else{
					$this->error('编辑失败！');
				}

			}
			
			//视频删除
			Public function del(){
				$video=M('Video');
				$videoAttach=M('VideoAttach');
				$videourl=M('Videourl');

				$res1 = $video->where(array('id'=>$_GET['id']))->delete();
				$res2 =	$videoAttach->where(array('vid'=>$_GET['id']))->delete();
				$res3 = $videourl->where(array('vid'=>$_GET['id']))->delete();

				if($res1 || $res2 || $res3){
					$this->success('删除成功！');
				}else{
					$this->error('删除失败！');
				}
			
			}
			
 		//图片上传
 		public function uploadify(){

 			if(md5($_POST['time'])==$_POST['token']){	//验证表单
	 			$files=new UploadModel;
	 			$res=$files->upload($_FILES,$_POST['type']);
	 			echo $res['savename'];	//AJAX返回值
 			}

 		}		
	}